﻿using AutoMapper;
using MediatR;
using System.Security.Principal;
using Traceability.App.Api.Read.Application.Command.Trace;
using Traceability.App.Domain.新文件夹;
using Traceability.App.ErrorCode;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Api.Read.Application.Handler.Trace
{
    public class DelHandler : IRequestHandler<DelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<TraceabilityModel> traceaRepo;
        private readonly IMapper mapper;

        public DelHandler(IBaseRepository<TraceabilityModel> traceaRepo, IMapper mapper)
        {
            this.traceaRepo = traceaRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var trace = traceaRepo.GetValue(request.Id).Result;
            if (trace == null)
            {
                res.Message = "用户不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            var entity = mapper.Map<TraceabilityModel>(trace);
            entity.IsDel = true;
            var r = traceaRepo.Update(entity).Result;
            if (r > 0)
            {
                res.Message = "删除成功";
                res.Code = ApiEnum.Ok;
                res.Data = r;
            }
            else
            {
                res.Message = "删除失败";
                res.Code = ApiEnum.Ok;
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
